package edu.nf.supermarket.service.impl;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import edu.nf.supermarket.entity.Commodity;
import edu.nf.supermarket.entity.CommodityImage;
import edu.nf.supermarket.entity.Orders;
import edu.nf.supermarket.mapper.CommodityMapper;
import edu.nf.supermarket.mapper.OrderMapper;
import edu.nf.supermarket.service.CommodityService;
import edu.nf.supermarket.util.OrderNOGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yjj
 * @date 2025-05-20
 */
@Service("commodityService")
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Commodity> findAllCommodity() {
        return commodityMapper.findAllCommodity();
    }

    @Override
    public List<Commodity> findCommodityByName(String commodityName) {
        return commodityMapper.findCommodityByName(commodityName);
    }

    @Override
    public List<Commodity> findCommodityByClassify(Integer classifyId) {
        return commodityMapper.findCommodityByClassify(classifyId);
    }

    @Override
    public Commodity findCommodityById(Integer commodityId) {
        return commodityMapper.findCommodityById(commodityId);
    }

    @Override
    public void addCommodityToCart(String orderId, Integer commodityId, Integer commodityNum) {
        commodityMapper.addCommodityToCart(orderId, commodityId, commodityNum);
    }

    @Override
    public void buyCommodity(Integer userId, Integer commodityId, Integer commodityNum, String spec1, String spec2) {
        Orders orders = new Orders();
        orders.setOrderId(OrderNOGenerator.generateOrderNO());
        orders.setCreateTime(LocalDateTime.now());
        orderMapper.addOrder(orders, userId);
        orderMapper.addOrderItem(orders.getOrderId(), commodityId, commodityNum,spec1,spec2);
    }

    @Override
    public List<Commodity> findRecommend() {
        List<Commodity> recommend = commodityMapper.findRecommend();
        return recommend.stream().map(c -> commodityMapper.findCommodityById(c.getCommodityId())).collect(Collectors.toList());
    }

    @Override
    public void addCommodity(Commodity commodity) {
        commodityMapper.addCommodity( commodity);

    }

    @Override
    public void addCommodityPic(Integer commodityId, String commodityPic) {
        commodityMapper.addCommodityPic(commodityId, commodityPic);
    }

    @Override
    public void updateCommodity(Commodity commodity) {
        commodityMapper.updateCommodity( commodity);
    }

    @Override
    public void updateCommodityPic(CommodityImage commodityImage) {
        commodityMapper.updateCommodityPic(commodityImage);
    }

    @Override
    public void updateCommodityStatus(Integer commodityId, Byte commodityStatus) {
        commodityMapper.updateCommodityStatus(commodityId,commodityStatus);
    }
}
